#遍历图片中每个像素点，并将对应的像素点更改为想要的值

import cv2
import os
color_map = {
    (1,1,1):(0, 0, 128),
    (2,2,2):(0, 128, 0),
    (3,3,3):(128, 0, 128),
    (4,4,4):(128,128,128),
    # (255,255,255):(0,0,0)
}

if __name__ == "__main__":
    # 读取图像
    img_path = r"C:\Users\luoluoluo\Desktop\final\ground\060_5805.png"
    save_path = os.path.join(os.path.dirname(img_path),os.path.basename(img_path).replace('.png', '_seg.png'))
    image = cv2.imread(img_path)

    # 检查图像是否成功载入
    if image is None:
        print("图像文件未能加载，请检查文件路径！")
    else:
        # 获取图像的高度和宽度
        height, width = image.shape[:2]
        # 遍历图像的每个像素
        for y in range(height):
            for x in range(width):
                # 获取当前像素点的颜色
                color = tuple(image[y, x])
                if color in color_map:
                    image[y, x] = color_map[color]
        # 保存修改后的图像
        cv2.imwrite(save_path, image)
